var Xhr = function(config) {
	for (var s in config)
		this[s] = config[s];
}
Xhr.prototype = {
	type : "GET",
	timeout : 30000,
	async : true,
	data : null,
	tran : false,
	states : ['Start', 'Init', 'Send', 'Receive', 'Complete[Success|Failure]'],
	contentType : "application/x-www-form-urlencoded",
	state : 'Start',
	accepts : {
		xml : "application/xml, text/xml",
		html : "text/html",
		script : "text/javascript, application/javascript",
		json : "application/json, text/javascript",
		text : "text/plain"
	},
	request : function(s) {
		for (var o in this) {
			if (!s[o])
				s[o] = this[o];
		}
		s.setParams(s);
		if (!s.noajax) {
			var xhr = window.ActiveXObject
					? new ActiveXObject("Microsoft.XMLHTTP")
					: new XMLHttpRequest();
			s.xhr = xhr;
			xhr.open(s.type, s.url, s.async);
			s.setHeader();
			xhr.onreadystatechange = function() {
				s.onStateChange();
			};
			xhr.send(s.data);
		}
		return s;
	},
	onStateChange : function() {
		this.readyState = this.xhr.readyState;
		if (this.readyState == 4) {
			this.responseText = this.xhr.responseText;
			this.handleResponse(this, this.responseText);
		} else {
			this.state = this.states[this.readyState];
		}
		var onFn = this['on' + this.state]

		if (onFn)
			onFn(this, this.responseText);
	},
	handleResponse : function(t, r) {
		var re = /^([^\[]*)\[([^|]*)\|([^\]]*)\]$/;
		s = this.states[this.readyState].match(re);
		if (this.isSucc()) {
			this.state = s[2];
		} else {
			this.state = s[3];
		}
	},
	setParams : function(s) {
		var p = s.baseParams, d = s.data;
		if (typeof d == "function") {
			d = d.call(s.scope || windwo, s);
		} else if (d.tagName) {
			var f = s.data, e = f.getAttribute("enctype");
			s.url = s.url || f.action;
			s.type = f.method || s.type;
			if (e && e.toLowerCase() == 'multipart/form-data') {
				return s.doFormUpload(s, p, s.url);
			}
			d = s.serializeForm(f);
		} else if (d == "cross" || d == 'crossDomain') {
			s.doCrossDomain(s);
		} else if (typeof d == "object") {
			// d = Ext.urlEncode(d);
		}
		if (p)
			s.data = d ? (d + '&' + p) : p;
		else 
		  s.data=d;
		if (s.data && s.type == "GET") {
			s.url += (s.url.match(/\?/) ? "&" : "?") + s.data;
			s.data = null;
		}
	},
	doCrossDomain : function(s) {
		if (!Xhr.id)
			Xhr.id = 0;
		var id = Xhr.id++, isIE = window.ActiveXObject;

		if (s.baseParams)
			s.url += (s.url.match(/\?/) ? "&" : "?") + s.baseParams;
		s.cb = "callback" + id;
		s.url += (s.url.match(/\?/) ? "&" : "?") + "callback=" + s.cb;
		if (s.nocache) {
			s.url += "&_dc=" + (new Date().getTime());
		}
		var head = document.getElementsByTagName("head")[0];
		var script = document.createElement("script");
		script.setAttribute("src", s.url);
		script.setAttribute("type", "text/javascript");
		script.setAttribute("id", "stcScript" + id);

		window[s.cb] = function(o) {
			// alert(o);
			s.responseText = o;
			if (s.onSuccess)
				s.onSuccess(s, o);
		};
		var complete = function() {
			if (!isIE || (this.readyState && this.readyState == "loaded")) {
				// 'loading、interactive、loaded'
				head.removeChild(document.getElementById("stcScript" + id));
				window[s.cb] = undefined;
			}
		}
		script.onreadystatechange = script.onload = complete;
		head.appendChild(script);
		s.noajax = true;

	},
	doFormUpload : function(s) {
		var doc, responseText, isIE = window.ActiveXObject;
		if (!Xhr.id)
			Xhr.id = 0;
		var id = Xhr.id++;
		var fram = "upload" + id;
		var frame = document.createElement('iframe');
		frame.id = frame.name = fram;
		frame.style.display = 'none';
		document.body.appendChild(frame);
		if (isIE) {
			document.frames[id].name = fram;// 不直接会弹出一个窗口出来马上消失
		}
		var form = s.data;
		form.target = fram;
		form.method = 'POST';
		form.enctype = 'multipart/form-data';

		if (s.url && s.baseParams) {
			form.action = us.rl
					+ (s.url.match(/\?/) ? "&" : "?" + s.baseParams);
			// alert(url + (url.match(/\?/) ? "&" : "?" + ps));
		} else
			form.action = s.url;
		var cb = function() {
			doc = frame.contentWindow.document || frame.contentDocument
					|| window.frames[id].document;
			if (doc && doc.body)
				responseText = doc.body.innerHTML;
			s.responseText = responseText;
			if (s.onSuccess)
				s.onSuccess(s, responseText);
		}
		if (isIE)
			frame.attachEvent('onload', cb);// 对于IE不能直接使用onload
		else
			frame.onload = cb;
		s.noajax = true;
		form.submit();// form中不应该出现submit
	},
	serializeForm : function(form) {
		var el, ename, evalue, spec, data = '';
		var encode = encodeURIComponent;
		var arr, opt, v, t, isIE = window.ActiveXObject;
		var re = /(select\w+)|(radio|checkbox)|(file|reset|button)/;
		for (var i = 0; i < form.elements.length; i++) {
			el = form.elements[i];
			ename = encode(el.name);
			evalue = encode(el.value);
			if (!el.disabled && el.name) {
				arr = re.exec(el.type);
				if (arr == null)
					data += ename + '=' + evalue + '&';
				else {
					if (arr[1]) {
						for (var j = 0; j < el.options.length; j++) {
							opt = el.options[j];
							v = opt.value;
							t = opt.text;
							if (isIE)
								spec = opt.attributes['value'].specified;
							else
								spec = opt.hasAttribute('value');
							if (opt.selected)
								data += ename + '=' + encode(spec ? v : t)
										+ '&';
						}
					} else if (arr[2]) {
						if (el.checked)
							data += ename + '=' + evalue + '&';
					}
				}
			}
		}
		data = data.substr(0, data.length - 1);
		return data;
	},
	setHeader : function() {
		this.xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
		this.xhr.setRequestHeader("Accept", "*/*");
		this.xhr.setRequestHeader("Accept", this.dataType
				&& this.accepts[this.dataType] ? this.accepts[this.dataType]
				+ ", */*" : "*/*");
		if (this.data) {
			this.xhr.setRequestHeader("Content-Type", this.contentType);
		}
		if (this.onSetHeader)
			this.onSetHeader(this, xhr);
	},
	isSucc : function() {
		if (this.xhr.readyState == 4 && this.xhr.status == 200) {
			return true;
		} else {
			return false;
		}
	},
	fillTo : function(s, el, cb) {
		s.el = el;
		s.onSuccess = function() {
			if (cb && cb(s, s.responseText) == false) {
			} else {
				el.innerHTML = s.responseText;
			}
		};
		return this.request(s);
	},
	getJson : function(s, cb) {
		s.onSuccess = function() {
			// alert("aaa");
			var data = eval("(" + s.responseText + ")");
			cb(s, data);
		};
		return this.request(s);
	},
	getScript : function(s, cb) {
		s.onSuccess = function() {
			var data = s.globalEval(s.responseText);
			cb(s, data);
		};
		return this.request(s);
	},
	getText : function(s, cb) {
		s.onSuccess = function() {
			cb(s, s.responseText);
		};
		return this.request(s);
	},
	crossDomain : function(s) {

	},
	globalEval : function(data) {

		if (data) {
			var head = document.getElementsByTagName("head")[0]
					|| document.documentElement, script = document
					.createElement("script");
			script.type = "text/javascript";
			if (window.ActiveXObject) {
				script.text = data;
			} else {
				script.appendChild(document.createTextNode(data));
			}
			head.insertBefore(script, head.firstChild);
			head.removeChild(script);
		}
	}

};